package com.zmc.member.controller;


import com.zmc.common.config.SysConfig;
import com.zmc.common.utils.file.FileUploadUtils;
import com.zmc.common.utils.file.MimeTypeUtils;
import com.zmc.member.application.MemberApplicationService;
import com.zmc.member.infrastructure.bean.query.LoginQuery;
import com.zmc.member.infrastructure.bean.vo.MemberCodeVo;
import com.zmc.member.infrastructure.bean.vo.MemberPhoneVo;
import com.zmc.member.infrastructure.bean.vo.MemberPwdVo;
import com.zmc.member.infrastructure.bean.vo.MemberVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zmc.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 会员
 *
 * @author zmc
 * @email 
 * @date 2024-10-09 14:56:33
 */
@RestController
public class MemberController {

    public static String upLoadPrefix = "http://localhost:8080/api";

    @Autowired
    private MemberApplicationService memberApplicationService;


    /**
     * 会员登录
     * */
    @PostMapping("/login")
    public R login(@RequestBody LoginQuery query) {
        return R.ok(memberApplicationService.login(query));
    }

    /**
     * 发送验证码，验证是否为本人手机号
     * */
    @PostMapping("/send/code")
    public R sendCode(@RequestBody MemberPhoneVo vo) {
        memberApplicationService.sendCode(vo);
        return R.ok();
    }

    /**
     * 输入验证码是否正确
     * */
    @PostMapping("/valid/code")
    public R validCode(@RequestBody MemberCodeVo vo) {
        memberApplicationService.validCode(vo);
        return R.ok();
    }


    @PostMapping("/update")
    public R updateBaseInfo(@RequestBody MemberVo vo) {
        memberApplicationService.updateMemberBaseInfo(vo);
        return R.ok();
    }

    /**
     * 获取会员信息
     * */
    @PostMapping("/info")
    public R info() {
        return R.ok(memberApplicationService.getInfo());
    }



    /**
     * 更换密码
     * */
    @PostMapping("/update/pwd")
    public R changePwd(@RequestBody MemberPwdVo vo) {
        memberApplicationService.changePwd(vo);
        return R.ok();
    }

    /**
     * 头像上传
     */
    @PostMapping("/avatar")
    public R avatar(@RequestParam("avatar") MultipartFile file) throws Exception {
        if (!file.isEmpty()) {
            String avatar = FileUploadUtils.upload(SysConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            return R.ok().put("imgUrl", upLoadPrefix + avatar);
        }
        return R.error("上传图片异常，请联系管理员");
    }

}
